﻿using System;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Collections;
using System.Net;
using System.Collections.Generic;

namespace NetAdapterTrace
{
    public partial class NetWorkAdapters : Form
    {
        public NetWorkAdapters()
        {
            InitializeComponent();
        }

        private void btnGetNwAdapters_Click(object sender, EventArgs e)
        {
            ShowNetworkInterfaces();
        }
        NetworkInterface[] ArrNwInterfaces = NetworkInterface.GetAllNetworkInterfaces();
       
        public void ShowNetworkInterfaces()
{
    IPHostEntry hostInfo = Dns.GetHostEntry(string.Empty  );
   bsList.Add ( NwAdapterInfo.GetIpAsString ( hostInfo.AddressList,bsList )); 
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
  
    List<NetworkInterface> lstNI = new List<NetworkInterface>(ArrNwInterfaces);
    List<NwAdapterInfo> lstNwAdapInfo = new List<NwAdapterInfo>();  
            
    dataGridView1.DataSource = lstNI;
    dgvNwInfo.DataSource = lstNwAdapInfo;
    bsList.Add(string.Format ("Interface information for {0}.{1}     ",
            computerProperties. HostName, computerProperties.DomainName));
    if (ArrNwInterfaces == null || ArrNwInterfaces.Length < 1)
    {
        bsList.Add("  No network interfaces found.");
        return;
    }
    treeView1.BeginUpdate();
    treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick); 
    bsList.Add(string.Format("  Number of interfaces .................... : {0}", ArrNwInterfaces.Length));
    foreach (NetworkInterface adapter in ArrNwInterfaces)
    {
        NwAdapterInfo globNwItem = new NwAdapterInfo(); 
        treeView1.Nodes.Add(adapter.Name);
         
        globNwItem =  NwAdapterInfo.DisplayAdapterInfo(adapter, bsList,globNwItem );
        lstNwAdapInfo.Add(globNwItem);
        cmbAdapter.Items.Add(globNwItem.Name);           

       
    }

    treeView1.EndUpdate();  
  }

        void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            NwAdapterInfo.DisplayAdapterInfo(ArrNwInterfaces[e.Node.Index], bsList,new  NwAdapterInfo()  );
            propGridNInfo.SelectedObject = ArrNwInterfaces[e.Node.Index];
        }
        private Timer timer;
        
        /// <summary>
        /// Initialize the Timer
        /// </summary>
        private void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = (int)2000;
            timer.Tick += new EventHandler(timer_Tick); 
            timer.Start();
        }
        IPv4InterfaceStatistics interfaceStats;
        NetworkInterface selectedNic = null;
        void timer_Tick(object sender, EventArgs e)
        {
          
            // Get the stats for that interface
            interfaceStats = selectedNic.GetIPv4Statistics();
            double bytesSentSpeed = (interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
            double bytesReceivedSpeed = (interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;
            lblBytesReceived.Text = interfaceStats.BytesReceived.ToString();
            lblBytesSent.Text = interfaceStats.BytesSent.ToString();
            lblUpload.Text = bytesSentSpeed.ToString() ;
            lblDownload.Text = bytesReceivedSpeed.ToString() ;

        }



        private void btnGetSpeed_Click(object sender, EventArgs e)
        {
            // Get NetworkInterface object that describes the current adapter

            selectedNic = ArrNwInterfaces[cmbAdapter.SelectedIndex];
            InitializeTimer();
           

            // Calculate the speed of bytes going in and out
           
            
            // Update the labels
            lblSpeed.Text = selectedNic.Speed.ToString();


        }

 

 


    }
}